var private const	Mensu_pos = 1;
var private const	Length_pos = 2;
var private point[2];

function private InputSetup()
{
	var	men[6], i, j, length, key, v;

	men = {"  20 面", "  80 面", " 320 面", " 1280面"," 5120面","20480面" };

	length = abs(Position(cur) - Position(point[0]));

	DlogOpen( "球作成 設定変更", 3, "ｷｬﾝｾﾙ ", " 決定 " );
	DlogString( 0, "半径", substr(tostring( length ), 8 ), 10 );
	DlogSelect( 2, "面数", men, Ball_level );
	key = DlogWait();
	if( key == 0 )
		return FALSE;
	else
	{
		j = DlogAnswer( 0 );
		if( j != substr(tostring( length ), 8 ))
		{
			if( MouseWindow() == WIN_YZ )
				v = vector( 0, 1, 0 );
			else
				v = vector( 1, 0, 0 );
			cur = point[0] + v*atof(j);
			Cursor( cur );
			Status_title[Length_pos] = "半径:"+ substr( j, 8 );
		}
		Ball_level = DlogAnswer( 2 );
		j = 20;
		for( i = 0;i<Ball_level;i++ )
			j = j*4;
		Status_title[Mensu_pos] = "面数: "+tostring( j, 6 )+" ";
		DrawStatus();
	}
}

//
//	Tamenのサブルーチン
//
function private MakePoly( length, level, v0, v1, v2, c )
{
	var	v3, v4, v5;
	if( level == 0 )
	{
		PolySetVertex( c + (v0*length), 0 );
		PolySetVertex( Vertex(v1*length)+c, 1 );
		PolySetVertex( Vertex(v2*length)+c, 2 );
		PolyVertexs( 3 );
		PolyType( POLY_SIMPLE );
		PolyAttr( AttrCurrent() );
		PolyObj( ObjCurrent() );
		PolyAppend( TRUE );
		DrawCurrent( TRUE );
	}
	else
	{
		v3 = unit(v0+v1);
		v4 = unit(v1+v2);
		v5 = unit(v2+v0);
		MakePoly( length, level-1, v0, v3, v5, c );
		MakePoly( length, level-1, v3, v4, v5, c );
		MakePoly( length, level-1, v1, v4, v3, c );
		MakePoly( length, level-1, v2, v5, v4, c );
	}
}

//
//	正２０面体を発生
//	length:半径
//	level:丸めレベル
//	c:中心座標
//
function private Tamen( length, level, c )
{
	var	theta;
	var	ue_ten[6], shita_ten[6];
	var	i;
	var	v[3];
	var	temp_vertex;

	theta = PI/2 - 2.0*acos( 0.5/sin(PI/5));
	for( i = 0;i<5;i++ )
	{
		ue_ten[i] = vector( cos(PI*2/5*i)*cos(theta), sin(PI*2/5*i)*cos(theta), sin(theta));
		shita_ten[i] = vector( cos(PI/5+PI*2/5*i)*cos(theta), sin(PI/5+PI*2/5*i)*cos(theta), -sin(theta));
	}
	ue_ten[5] = ue_ten[0];
	shita_ten[5] = shita_ten[0];

	for(i = 0;i<5;i++ )
	{
		v[0] = vector( 0, 0, 1 );
		v[1] = ue_ten[i];
		v[2] = ue_ten[i+1];
		MakePoly( length, level, v[0], v[1], v[2], c );
	}
	for( i = 0;i<5;i++ )
	{
		v[0] = ue_ten[i];
		v[1] = shita_ten[i];
		v[2] = ue_ten[i+1];
		MakePoly( length, level, v[0], v[1], v[2], c );
		v[0] = ue_ten[i+1];
		v[1] = shita_ten[i];
		v[2] = shita_ten[i+1];
		MakePoly( length, level, v[0], v[1], v[2], c );
	}
	for(i = 0;i<5;i++ )
	{
		v[0] = vector( 0, 0, -1 );
		v[1] = shita_ten[i+1];
		v[2] = shita_ten[i];
		MakePoly( length, level, v[0], v[1], v[2], c );
	}
}

//
//	球作成
//
function CreateBall()
{
	var temp, i, length;
	var vers, key, prev_sel;
	var suu[6];
	var pick ,draw_flag = ON;

	length = 0.0;
	suu[0] = 20;
	for( i = 1;i<6;i++ )
		suu[i] = suu[i-1]*4;

	PushMenu();
	Status_org = Status_title;
	ClearStatus();
	MenuPosition( Menu( " 動作設定(&S)",
			"設定変更",		InputSetup,
			"--------",		SEPARATE_MENU,
			"作成中止	ESC",	MenuQuit
		), Menu_Title );

	prev_sel = Select();
	quit_flag = FALSE;

	Status_title[0] = "【球作成中】";
	Status_title[Mensu_pos] = "面数: "+tostring(suu[Ball_level], 6 )+" ";
	Status_title[Length_pos] = "半径:"+substr(tostring( length ),8);
	DrawStatus();

	point[0] = Cursor();
	cur = point[0];
	pick = cur;
	vers = 1;
	input_flag = OFF;
	while( TRUE )
	{
		if( draw_flag == ON )
			DrawLine( point[0], cur );
		key = WaitEvent();
		if( key )
			input_flag = ON;
		if(( ShiftStat() & 1 ) == 1)
		{
			cur = Cursor();
			TrimCursor(point[0], MouseWindow());
			Cursor( cur );
		}
		else
			cur = Cursor();
		key = KeyCode();
		if( input_flag | key == INPUT_KEY )
		{
			point[vers] = cur;
			if( cur == point[0] )
			{
				Warning();
				if( novice_flag == ON )
					Message( "最初に指定した点と同じ点です\n"
						+"２点目は半径を指定してください" );
			}
			else
				break;
		}
		else if( key == ESC | key == BS | quit_flag == TRUE )
		{
			PopandClear( OVERWRITE );
			return;
		}
		else if( key == KEY_ROLLUP )
		{
			if( Ball_level < 5 )
			{
				Ball_level++;
				Status_title[Mensu_pos] =
					"面数: "+tostring(suu[Ball_level], 6 )+" ";
			}
			else
			{
				Warning();
				if( novice_flag == ON )
					Message( "球の面数はこれ以上は増やせません" );
			}
		}
		else if( key == KEY_ROLLDOWN )
		{
			if( Ball_level > 0 )
			{
				Ball_level--;
				Status_title[Mensu_pos] =
					"面数: "+tostring(suu[Ball_level], 6 )+" ";
			}
			else
			{
				Warning();
				if( novice_flag == ON )
					Message( "球の面数はこれ以下には減らせません" );
			}
		}
		else
			temp = KeyProcess( key );
		length = abs(Position( cur )- Position(point[0]));
		Status_title[Length_pos] = "半径:"+substr(tostring( length ),8);
		DrawStatus();
		if( ((key!=0 ) & (key != INPUT_KEY)) | ( pick != Cursor()) )
		{
			draw_flag = ON;
			DrawLine( point[0], pick );
		}
		else
			draw_flag = OFF;
		pick = Cursor();
		input_flag = OFF;
	}
	Clear( OVERWRITE );
	if( SelectPolygons( prev_sel ) != 0 )
	{
		SelectAllFalse();
		SelectAll( FALSE );
	}
	length = abs( Position(point[1]) - Position(point[0] ));
	ViewCursor( OFF );
	Tamen( length, Ball_level, point[0] );
	ViewCursor( ON );
	UpdateObject();
	pers_rotation_flag = FALSE;
	PopMenu();
	Status_title = Status_org;
	DrawStatus();
}
